package com.gzx.spring.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gzx.risk.core.pojo.UserLevelResultInfo;
import com.gzx.spring.db.entity.UserScoreResult;
import com.gzx.spring.db.mapper.UserScoreResultMapper;
import com.gzx.spring.db.service.UserScoreResultService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 用户分值评定表 服务实现类
 * </p>
 *
 * @author qinpeng
 * @since 2025-10-23
 */
@Service
public class UserScoreResultServiceImpl extends ServiceImpl<UserScoreResultMapper, UserScoreResult> implements UserScoreResultService {

    @Override
    public void saveUserScoreResult(UserLevelResultInfo resultInfo) {
        UserScoreResult info = new UserScoreResult();
        BeanUtils.copyProperties(resultInfo,info);
        info.setCreatedTime(LocalDateTime.now());
        info.setUpdatedTime(LocalDateTime.now());
        this.save(info);
    }

    @Override
    public UserLevelResultInfo getInfoByIdCard(String idCard){
        LambdaQueryWrapper<UserScoreResult> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserScoreResult::getIdCard,idCard);
        UserScoreResult result = getOne(wrapper, false);
        return toInfo(result);
    }

    @Nullable
    private static UserLevelResultInfo toInfo(UserScoreResult result) {
        if(result == null){
            return null;
        }
        UserLevelResultInfo resultInfo = new UserLevelResultInfo();
        BeanUtils.copyProperties(result,resultInfo);
        return resultInfo;
    }

    @Override
    public UserLevelResultInfo getInfoByOrderCode(String orderCode) {
        LambdaQueryWrapper<UserScoreResult> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserScoreResult::getOrdercode,orderCode);
        UserScoreResult result = getOne(wrapper, false);
        return toInfo(result);
    }

    @Override
    public UserLevelResultInfo getUserScoreResultByUid(String uid) {
        LambdaQueryWrapper<UserScoreResult> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserScoreResult::getUid,uid);
        UserScoreResult result = getOne(wrapper, false);
        return toInfo(result);
    }
}
